<?php
namespace Intendant\Controller;
use Think\Controller;
class SiteComController extends Controller{
	public function _initialize()
    {
        if(!isset($_SESSION['admin_uid']))
        {
            $this->redirect('Login/index');
        }
    }
    public function treeMenu(){
		if(IS_POST){
			$Rule = M("AuthRule")->field("id,title as text,pid")->order("sort asc")->select();
			$treeRule[0] = array(
				'id' => 0,
				'text' => '顶级菜单',
				'children' => treeRule($Rule)
			);
			$this->ajaxReturn($treeRule);
			exit;
		}
	}
    //读取系统图标大全
	public function iconsCls(){
		if(IS_POST){
			$iconsCls = file_get_contents("./Public/intendant/css/icons.css");
			$iconsCls = explode('}', $iconsCls);
			$tmp_iconsCls = array();
			foreach($iconsCls as $k => $v){
				if(preg_match("/\.(.+?){/", $v,$m)){
					$tmp_iconsCls[] = $m[1];
				}
			}
			$this->ajaxReturn($tmp_iconsCls);
		}
	}
	//验证添加菜单时是否已存在菜单名
	public function checkAddMTit(){
		if(IS_POST && I('title')){
			$count = M("AuthRule")->where(array("title"=>I("title")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//验证修改菜单时是否已存在菜单名
	public function checkEditMTit(){
		if(IS_POST && I('title')){
			$where = array(
				'id' => array('neq',I("get.id")),
				'title' => I("title")
			);
			$AuthRule = M("AuthRule");
			$count = $AuthRule->where($where)->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//验证添加菜单时是否已存在规则
	public function checkAddMenu(){
		if(IS_POST && I('name')){
			$count = M("AuthRule")->where(array("name"=>I("name")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//验证修改菜单时是否已存在规则
	public function checkEditMenu(){
		if(IS_POST && I('name')){
			$where = array(
				'id' => array('neq',I("get.id")),
				'name' => I("name")
			);
			$AuthRule = M("AuthRule");
			$count = $AuthRule->where($where)->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//添加账号时验证是否已存在用户
	public function checkUsername(){
		if(IS_POST && I('username')){
			$count = M("User")->where(array("username"=>I("username")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//添加帐号时验证是否已存在昵称
	public function checkNickname(){
		if(IS_POST && I('nickname')){
			$count = M("User")->where(array("nickname"=>I("nickname")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	public function checkEmail(){
		if(IS_POST && I('email')){
			$count = M("User")->where(array("email"=>I("email")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//编辑用户时验证是否已存在用户名
	public function checkEditUsername(){
		if(IS_POST && I('username')){
			$where = array(
				'uid' => array('neq',I("get.uid")),
				'username' => I("username")
			);
			$User = M("User");
			$count = $User->where($where)->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//编辑用户时验证是否已存在昵称
	public function checkEditNickname(){
		if(IS_POST && I('nickname')){
			$where = array(
				'uid' => array('neq',I("get.uid")),
				'nickname' => I("nickname")
			);
			$User = M("User");
			$count = $User->where($where)->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//编辑邮箱时验证是否已存在昵称
	public function checkEditEmail(){
		if(IS_POST && I('email')){
			$where = array(
				'uid' => array('neq',I("get.uid")),
				'email' => I("email")
			);
			$User = M("User");
			$count = $User->where($where)->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//添加角色时验证是否已存在角色名称
	public function checkRole(){
		if(IS_POST && I('title')){
			$count = M("AuthGroup")->where(array("title"=>I("title")))->count();;
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	//编辑角色时验证是否已存在角色名称
	public function checkRoleTitle(){
		if(IS_POST && I('title')){
			$where = array(
				'id' => array('neq',I("get.id")),
				'title' => I("title")
			);
			$Group = M("AuthGroup");
			$count = $Group->where($where)->count();
			if(!$count){
				echo 'true';
			} else {
				echo 'false';
			}
			exit;
		}
	}
	// 获取用户角色列表
	public function listGroup(){
		$table_AuthGroup = M("AuthGroup")->order('sort asc')->select();
		if(IS_POST && I("get.type") == "all"){
			array_unshift($table_AuthGroup,array("id"=>"0","title"=>"全部角色","selected"=>true));

		}
		$this->ajaxReturn($table_AuthGroup);
		exit;
	}
}
?>